home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / CONVERTERS.PY < prev    next >
Encoding:
Python Source  |  2000-07-24  |  6.6 KB  |  192 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. __version__='$Revision: 1.7.38.2 $'[11:-2]
  65.  
  66. import regex
  67. from string import atoi, atol, atof, join, split, strip
  68. from types import ListType, TupleType
  69.  
  70. def field2string(v):
  71.     if hasattr(v,'read'): v=v.read()
  72.     else: v=str(v)
  73.     return v
  74.  
  75. def field2text(v, nl=regex.compile('\r\n\|\n\r').search):
  76.     if hasattr(v,'read'): v=v.read()
  77.     else: v=str(v)
  78.     l=nl(v)
  79.     if l < 0: return v
  80.     r=[]
  81.     s=0
  82.     while l >= s:
  83.         r.append(v[s:l])
  84.         s=l+2
  85.         l=nl(v,s)
  86.     r.append(v[s:])
  87.         
  88.     return join(r,'\n')
  89.  
  90. def field2required(v):
  91.     if hasattr(v,'read'): v=v.read()
  92.     else: v=str(v)
  93.     if strip(v): return v
  94.     raise ValueError, 'No input for required field<p>'
  95.  
  96. def field2int(v):
  97.     if type(v) in (ListType, TupleType):
  98.         return map(field2int, v)
  99.     if hasattr(v,'read'): v=v.read()
  100.     else: v=str(v)
  101.     if v:
  102.         try: return atoi(v)
  103.         except ValueError:
  104.             raise ValueError, (
  105.                 "An integer was expected in the value '%s'" % v
  106.                 )
  107.     raise ValueError, 'Empty entry when <strong>integer</strong> expected'
  108.  
  109. def field2float(v):
  110.     if type(v) in (ListType, TupleType):
  111.         return map(field2float, v)
  112.     if hasattr(v,'read'): v=v.read()
  113.     else: v=str(v)
  114.     if v:
  115.         try: return atof(v)
  116.         except ValueError:
  117.             raise ValueError, (
  118.                 "A floating-point number was expected in the value '%s'" % v
  119.                 )
  120.     raise ValueError, (
  121.         'Empty entry when <strong>floating-point number</strong> expected')
  122.  
  123. def field2long(v):
  124.     if type(v) in (ListType, TupleType):
  125.         return map(field2long, v)
  126.     if hasattr(v,'read'): v=v.read()
  127.     else: v=str(v)
  128.  
  129.     # handle trailing 'L' if present.
  130.     if v[-1:] in ('L', 'l'):
  131.         v = v[:-1]
  132.     if v:
  133.         try: return atol(v)
  134.         except ValueError:
  135.             raise ValueError, (
  136.                 "A long integer was expected in the value '%s'" % v
  137.                 )
  138.     raise ValueError, 'Empty entry when <strong>integer</strong> expected'
  139.  
  140. def field2tokens(v):
  141.     if hasattr(v,'read'): v=v.read()
  142.     else: v=str(v)
  143.     return split(v)
  144.  
  145. def field2lines(v):
  146.     return split(field2text(v),'\n')
  147.  
  148. def field2date(v):
  149.     from DateTime import DateTime
  150.     if hasattr(v,'read'): v=v.read()
  151.     else: v=str(v)
  152.     return DateTime(v)
  153.  
  154. def field2boolean(v):
  155.     return v
  156.  
  157. type_converters = {
  158.     'float':    field2float,
  159.     'int':      field2int,
  160.     'long':     field2long,
  161.     'string':   field2string,
  162.     'date':     field2date,
  163.     'required': field2required,
  164.     'tokens':   field2tokens,
  165.     'lines':    field2lines,
  166.     'text':     field2text,
  167.     'boolean':     field2boolean,
  168.     }
  169.  
  170. get_converter=type_converters.get
  171.